import org.newdawn.slick.*;
import org.newdawn.slick.geom.*;

/**
 * Used to control the player character in the game.
 *
 * @author Gerrit Jamerson, CodeMonkeys
 * @version 4/3/13
 */
import org.newdawn.slick.*;

public class PlayerObject extends GameObject
{
    Animation walkAnimationLeft, walkAnimationRight, standAnimationLeft, standAnimationRight, curAnimation;
    public PlayerObject()
    {
        super("PlayerObject");
        SpriteSheet walkSheet = GameRegistry.getSpriteSheet("anim_ArcherWalk");
        walkAnimationRight = SpriteSheetInfo.createAnimation(walkSheet, 100);
        walkAnimationLeft = SpriteSheetInfo.createFlippedAnimation(walkSheet, 100);

        SpriteSheet standSheet = GameRegistry.getSpriteSheet("anim_ArcherStand");
        standAnimationRight = SpriteSheetInfo.createAnimation(standSheet, 100);
        standAnimationLeft = SpriteSheetInfo.createFlippedAnimation(standSheet, 100);
        curAnimation = walkAnimationRight;

        enableCollisions(new BoundBox(-32f, -32f, 64f, 64f));
        //disableObjectCollision(new TileWall());
        disableClassCollision("TileWall");
        for(int i = 0; i < 100; i++)
        {
            for(int o = 0; o < 200; o++)
            {
                new TileWall(new Vector2f(200f + (i * 64),200f + (o * 64)));
            }
        }
    }

    /**
     * Called each step on singleplayer
     * Implementation TBD
     */
    public void spStep(GameContainer gameContainer, int intDelta)
    {
    }

    /**
     * Called each step on the client in multiplayer
     * Implementation TBD
     */
    public void mpClientStep(GameContainer gameContainer, int intDelta)
    {

    }

    /**
     * Called each step on the server
     * Implementation TBD
     */
    public void mpServerStep(GameContainer gameContainer, int intDelta)
    {

    }

    /**
     * Called each frame, used to draw the gameObject
     * Implementation TBD
     */
    float offsetY = 0;
    boolean facingRight = true;
    public void render(GameContainer gameContainer, Graphics graphics)
    {
        float targetY = (float)Math.sin((float)(((float)curAnimation.getFrame() + 1.0f) / (float)curAnimation.getFrameCount()) * (2*Math.PI)) * 2.0f;
        //offsetY += (targetY - offsetY) / 300.0f;
        offsetY = targetY;
        if(getVelocity().x < 0)
        {
            curAnimation = walkAnimationLeft;
            facingRight = false;
        } //End if
        else if(getVelocity().x > 0)
        {
            curAnimation = walkAnimationRight;
            facingRight = true;
        } //End else

        if(((int)(getVelocity().x * 100f)) == 0 && ((int)(getVelocity().y * 100f)) == 0)
        {
            if(facingRight)
            {
                curAnimation = standAnimationRight;
            } //End if
            else
            {
                curAnimation = standAnimationLeft;
            } //End else

            offsetY = 0;
            targetY = 0;
        } //End else

        curAnimation.draw(getPosition().x, getPosition().y - (float)Math.abs(offsetY), 64f, 64f);
        getBounds().draw(getPosition(), graphics);
    } //End render method

    /**
     * Called when the object collides with something
     */
    public void onCollision(CollisionData collision)
    {
        GameController.exitGame();
    }

    /**
     * Returns the current animation.
     * @pre None
     * @param None
     * @return The current animation
     * @post None
     */
    public Animation getCurrentAnimation()
    {
        return curAnimation;
    } //End getCurrentAnimation method
} //End class
